PHP & MySQL: Novice to Ninja by Kevin Yank

PHP & MySQL: Novice to Ninja by Kevin Yank

Author:Kevin Yank [Yank, Kevin]
Language: eng
Format: epub, mobi
ISBN: 9780987153081
Publisher: SitePoint Pty. Ltd.
Published: 2012-04-10T16:00:00+00:00


Note: Regular Expressions in Double Quoted Strings

All the regular expressions we’ve seen so far in this chapter have been expressed as single-quoted PHP strings. The automatic variable substitution provided by PHP strings is sometimes more convenient, but they can cause headaches when used with regular expressions. Double-quoted PHP strings and regular expressions share a number of special character escape codes. "\n" is a PHP string containing a newline character. Likewise, /\n/ is a regular expression that will match any string containing a newline character. We can represent this regular expression as a single-quoted PHP string ('/\n/') and all is well, because the code \n has no special meaning in a single-quoted PHP string. If we were to use a double-quoted string to represent this regular expression, we’d have to write "/\\n/"—with a double-backslash. The double-backslash tells PHP to include an actual backslash in the string, rather than combining it with the n that follows it to represent a newline character. This string will therefore generate the desired regular expression, /\n/. Because of the added complexity it introduces, it’s best to avoid using double-quoted strings when writing regular expressions. Note, however, that I have used double quotes for the replacement strings ("\n") passed as the second parameter to preg_replace. In this case, we actually do want to create a string containing a newline character, so a double-quoted string does the job perfectly.

With our line breaks all converted to newline characters, we can convert them to paragraph breaks (when they occur in pairs) and line breaks (when they occur alone): // Paragraphs $text = '<p>' . preg_replace('/\n\n/', '</p><p>', $text) . '</p>'; // Line breaks $text = preg_replace('/\n/', '<br>', $text);

Note the addition of <p> and </p> tags surrounding the joke text. Because our jokes may contain paragraph breaks, we must make sure the joke text is output within the context of a paragraph to begin with. This code does the trick: the line breaks in the text will now become the natural line- and paragraph-breaks expected by the user, removing the requirement to learn anything new to create this simple formatting. It turns out, however, that there’s a simpler way to achieve the same result in this case—there’s no need to use regular expressions at all! PHP’s str_replace function works a lot like preg_replace, except that it only searches for strings instead of regular expression patterns:$newString = str_replace(searchFor, replaceWith, oldString);

We can therefore rewrite our line-breaking code as follows: chapter8/includes/helpers.inc.php (excerpt)

// Convert Windows (\r\n) to Unix (\n) $text = str_replace("\r\n", "\n", $text); // Convert Macintosh (\r) to Unix (\n) $text = str_replace("\r", "\n", $text); // Paragraphs $text = '<p>' . str_replace("\n\n", '</p><p>', $text) . '</p>'; // Line breaks $text = str_replace("\n", '<br>', $text);



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.